// mircoApp/giftPackage/giftGoodsList/giftGoodsList.js
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
let app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    packageId:'', //套餐ID
    pageTemplate:'', //切换样式参数
    giftList:[],
    popupSpecs:false,
    goodData:{}, //规格弹窗数据
    goodinfo:{}, //商品详情数据
    showgp: false,//商品详情弹窗
    optionId:'',
    giftId:'',
    imgheights:[],
    imgBoxSize:'',
    getGoods:{},
    language:"",
    totalPrice:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      packageId:options.id
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },
  //提交订单
  submitGoods() {
    let json = {};
    let cartIds = [];
    let goodBol = true;
    json.package_id = this.data.packageId;
    json.goods_list = [];
    this.data.giftList.map((item,index) => {
      if (
        !this.data.pageTemplate && 
        item.goods.has_option==1 && 
        !item.goods.optionsId && 
        item.goods.has_many_specs.length > 0
      ) {
        goodBol = false;
      }
      let goodsJson = {};
      goodsJson.goods_id = item.goods?.id;
      goodsJson.option_id = '';
      if(item.goods?.has_option) {
        goodsJson.option_id = item.goods?.optionsId;
      }
      cartIds.push(item.id);
      goodsJson.total = 1;
      json.goods_list.push(goodsJson);
    })
    if(!goodBol) {
      app.tips(`请选择商品规格`)
      return false;
    }
    if (cartIds.length<=0) return app.tips(`请选择结算商品`);
    wx.navigateTo({
      url: `/packageD/buy/myOrder_v2/myOrder_v2?tag=giftBuy&goods_ids=${JSON.stringify(cartIds)}&packagJson=${JSON.stringify(json)}` 
    });
  },
  giftData() {
    let url = app.getNetAddresss("plugin.goods-package.frontend.package.package.combination-goods");
    let json = {
      package_id: this.data.packageId,
    }
    app._postNetWork({
      showToastIn: false,
      url,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        let data = res.data;
        if (res.result) {
          data.list.forEach((item,index) => {
            // item.goods.optionsId = '';
            item.goods.goodsnum = 1;
            item.goods.popStock = item.goods.stock;
            if(item.goods.has_option && data.page_template){ //默认选择第一个
              item.goods.optionsId = item.goods.has_many_options[0].id;
            }
          })
          this.setData({
            giftList:data.list,
            totalPrice:data.package_price,
            pageTemplate:data.page_template
          })
          
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  goodsSpescOn(event) {
    let goodsitem = event.currentTarget.dataset.goods;
    this.setData({
      getGoods:goodsitem,
      popupSpecs:true
    })
  },
  close_yz_specs_popup(event) {
    let {clicktype,goodsCount,optionsId} = event.detail;
    this.setData({
      popupSpecs: false,
    });
    if (clicktype == 1) {
      //点击确认按钮
      this.data.giftList.forEach((item,index) => {
        item.goods.has_many_options.forEach((optionItem,optionIndex) => {
          if(optionItem.id === optionsId) {
            this.setData({
              [`giftList[${index}].goods.stock`]:optionItem.stock,
              [`giftList[${index}].goods.count`]:goodsCount,
              [`giftList[${index}].goods.optionsId`]:optionsId,
              [`giftList[${index}].goods.price`]:optionItem.product_price,
              [`giftList[${index}].goods.optionName`]:optionItem.title,
            })
          }
        })
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.giftData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})